<?php

namespace app\controller;

use app\BaseController;
use app\common\exception\ApiException;
use app\common\RedisKey;
use app\facade\RedisLocationFacade;
use app\model\article\ArticleApplyModel;
use app\model\article\ArticleModel;
use app\model\coach\CoachRatioModel;
use app\model\config\RegionModel;
use app\model\goods\OrderModel;
use app\model\partner\EscortMenuModel;
use app\model\partner\EscortMenuOrderModel;
use app\model\partner\EscortMenuReserveOrderModel;
use app\model\partner\PartnerBusyTimeModel;
use app\model\partner\UserPartnerModel;
use app\model\school\SchoolModel;
use app\model\school\SchoolOrderModel;
use app\model\user\PartnerWithdrawRecordModel;
use app\model\user\UserCouponModel;
use app\model\user\UserEvaluateModel;
use app\model\user\UserModel;
use app\service\config\ConfigService;
use app\service\pay\PayService;
use app\service\user\CoachMoneyDetailService;
use app\service\user\PartnerMoneyRecordService;
use app\service\user\UserPartnerService;
use app\service\user\UserService;
use app\service\utils\MailService;
use app\service\utils\QqMapService;
use app\service\utils\QrcodeService;
use app\service\utils\RedisService;
use app\service\utils\SmsService;
use app\service\utils\TokenService;
use app\service\utils\TopicService;
use app\service\utils\WeiXinOfficialService;
use app\service\utils\WeiXinPayService;
use app\service\utils\WeiXinService;
use app\validate\user\UserRegisterValidate;
use app\validate\user\UserRetrieveValidate;
use DateTime;
use DateTimeZone;
use EasyWeChat\Factory;
use think\App;
use think\db\Query;
use think\facade\Config;
use think\facade\Db;
use think\facade\Log;
use think\facade\Queue;
use think\Request;
use think\Validate;
use zjkal\TimeHelper;
use app\service\order\VerifyOrderService;


class IndexController extends BaseController
{

    /**
     * 默认访问
     * @return string
     */
    public function index()
    {

        return $this->success();
    }

    public function test()
    {
        $res = [];
        Db::startTrans();
        try {
            return $this->success($res);
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }

    function sendSmsBaoSms($phone, $code)
    {
        $arr = [
            30 => '错误密码',
            40 => '账号不存在',
            41 => '余额不足',
            43 => 'IP地址限制',
            50 => '内容含有敏感词',
            51 => '手机号码不正确',
        ];
        $username = 'r1jw1';
        $password = 'aafde9c46b574c8dbae5b7bf7e0d2575';
        $content = "【灿乐祥科技】您的验证码是{$code}";
        $content = urlencode($content);
        $url = "https://api.smsbao.com/sms?u=$username&p=$password&m=$phone&c=$content";
        $code = curl_get($url);
        if ($code != 0) {
            throw new \Exception($arr[$code]);
        }
        return true;
    }

    public function clear()
    {
        try {
            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }

    /**
     * 延迟队列
     */
    public function testQueue()
    {
        //当前任务将由哪个类来负责处理。
        //当轮到该任务时，系统将生成一个该类的实例，并默认调用其 fire 方法
        $jobHandlerClassName = 'app\job\OrderJob';

        //当前任务归属的队列名称，如果为新队列，会自动创建
        //php think queue:work --queue orderJobQueue
        //php think queue:work --queue orderJobQueue --daemon

        $jobQueueName = "orderJobQueue";

        //数组数据
        $orderData = ['id' => uniqid(), 'time' => time(), 'message' => 'later message83'];

        //将该任务推送到消息队列，等待对应的消费者去执行
        //这里只是负责将数据添加到相应的队列名称的队列里，消费者与生产者并无联系

        //立即执行
        //        $isPushed = Queue::push($jobHandlerClassName, $orderData, $jobQueueName);
        //延迟10秒后执行
        $isPushed = Queue::later(30, $jobHandlerClassName, $orderData, $jobQueueName);

        if ($isPushed !== false) {
            echo date('Y-m-d H:i:s') . " 队列添加成功";
        } else {
            echo '队列添加失败';
        }
    }

    /**
     * redis
     * @return \think\response\Json
     */
    public function testRedis()
    {
        $client = RedisService::instance();
        $client->set('test', 1111);
        $result = $client->get('test');
        return $this->success($result);
    }

    /**
     * 测试中间件
     * @param Request $request
     * @return \think\response\Json
     */
    public function testAuthLogin(Request $request)
    {
        $data = (new TokenService())->generateToken(110);
        return $this->success($data);
    }

    /**
     * 测试中间件
     * @param Request $request
     * @return \think\response\Json
     */
    public function testAuth(Request $request)
    {
        return $this->success($request->userId);
    }
}
